package com.betterjr.modules.temhandler.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.CacheUtils;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.mapper.mapperhelper.EntityHelper;
import com.betterjr.mapper.mapperhelper.EntityHelper.EntityColumn;
import com.betterjr.modules.temhandler.dao.TemHandlerConfigMapper;
import com.betterjr.modules.temhandler.data.TemDataConstant;
import com.betterjr.modules.temhandler.data.TemHandlerConfigConstant;
import com.betterjr.modules.temhandler.data.TemRelationConstant;
import com.betterjr.modules.temhandler.dubbo.interfaces.IApplyTemDataService;
import com.betterjr.modules.temhandler.dubbo.interfaces.IAuditTemDataService;
import com.betterjr.modules.temhandler.dubbo.interfaces.ICheckTemDataService;
import com.betterjr.modules.temhandler.dubbo.interfaces.IDeleteTemDataService;
import com.betterjr.modules.temhandler.dubbo.interfaces.IEffectiveTemDataService;
import com.betterjr.modules.temhandler.entity.TemData;
import com.betterjr.modules.temhandler.entity.TemHandlerConfig;
import com.google.common.collect.Lists;

/**
 * 
 * @ClassName: TemHandlerConfigService 
 * @Description: 临时配置Service
 * @author xuyp
 * @date 2018年1月9日 下午2:52:54 
 *
 */
@Service
public class TemHandlerConfigService extends BaseService<TemHandlerConfigMapper, TemHandlerConfig>
        implements ApplicationContextAware, InitializingBean {

    private static ApplicationContext applicationContext;

    /**
     * 
     * @Title: checkVerfiyData 
     * @Description: 校验临时数据 
     * @param anTemData 校验临时数据
     * @throws 
     * @author xuyp
     * @date 2017年12月21日 上午10:50:12
     */
    public void checkVerfiyData(TemData anTemData) {

        BTAssert.notNull(anTemData, "数据为空,操作失败");
        String businTableType = anTemData.getBusinTableType();
        BTAssert.isTrue(StringUtils.isNoneBlank(businTableType), "请绑定数据类型,操作失败");

        TemHandlerConfig handlerConfig = findConfigByBusinTableType(businTableType);
        BTAssert.notNull(handlerConfig, "请先配置业务信息,操作失败");

        // 校验审核数据
        checkVerfiyDataPart(anTemData, handlerConfig, TemHandlerConfigConstant.CONFIGHANDLER_CHECKHANDLERMODEL_AUDIT);
        // 校验复核数据
        checkVerfiyDataPart(anTemData, handlerConfig, TemHandlerConfigConstant.CONFIGHANDLER_CHECKHANDLERMODEL_CHECK);

        // 校验立即申请
        checkVerfiyDataPart(anTemData, handlerConfig,
                TemHandlerConfigConstant.CONFIGHANDLER_CHECKHANDLERMODEL_ADDORUPDATE);

    }

    /**
     * 
     * @Title: findConfigByBusinTableType 
     * @Description:根据类的全路径查询当前类的配置
     * @param anTableType
     * @return 根据类的全路径查询当前类的配置
     * @throws 
     * @author xuyp
     * @date 2017年12月25日 上午9:17:00
     */
    @SuppressWarnings("unchecked")
    public TemHandlerConfig findConfigByBusinTableType(String anTableType) {

        BTAssert.isTrue(StringUtils.isNoneBlank(anTableType), "请绑定数据类型,操作失败");
        try {

            Object object = CacheUtils.get(TemHandlerConfigConstant.CONFIGHANDLER_STOR_REDIS_CACHE_NAME,
                    TemHandlerConfigConstant.CONFIGHANDLER_STORE_REDIS_PRIMARTKEY_PREFIX + anTableType);
            if (object != null) {
                return JSONObject.parseObject(object.toString(), TemHandlerConfig.class);
            }
        }
        catch (Exception e) {

            logger.info("从redis中获取配置信息出错  :" + anTableType + "   异常为：" + e.getMessage());
        }

        Map<String, Object> map = QueryTermBuilder.newInstance().put("businTableType", anTableType)
                .put("businStatus", TemHandlerConfigConstant.CONFIGHANDLER_BUSINSTATUS_EFFECITVE).build();

        List<TemHandlerConfig> selectByProperty = this.selectByProperty(map);
        if (Collections3.isEmpty(selectByProperty)) {
            BTAssert.notNull(null, "请先配置业务信息,操作失败");
        }

        if (selectByProperty.size() > 1) {
            BTAssert.notNull(null, "生效的配置业务信息过多,操作失败");
        }

        TemHandlerConfig handlerConfig = Collections3.getFirst(selectByProperty);
        // 往redis中存放数据

        try {
            if (handlerConfig != null) {

                Object jsonData = JSONObject.toJSON(handlerConfig);
                CacheUtils.put(TemHandlerConfigConstant.CONFIGHANDLER_STOR_REDIS_CACHE_NAME,
                        TemHandlerConfigConstant.CONFIGHANDLER_STORE_REDIS_PRIMARTKEY_PREFIX + anTableType, jsonData);
            }

        }
        catch (Exception e) {

            logger.info("从redis中写入配置信息出错  :" + anTableType + "   异常为：" + e.getMessage());
        }

        return handlerConfig;

    }

    /**
     * 
     * @Title: saveSynchronizedAddBusin 
     * @Description: 数据新增或者编辑时调用业务接口回写数据
     * @param anTemData
     * @param addFlag  true 新增     false  编辑
     * @throws 
     * @author xuyp
     * @date 2017年12月27日 下午2:55:01
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void saveSynchronizedAddBusin(TemData anTemData, boolean addFlag) {

        BTAssert.notNull(anTemData, "数据为空,操作失败");
        String businTableType = anTemData.getBusinTableType();
        BTAssert.isTrue(StringUtils.isNoneBlank(businTableType), "请绑定数据类型,操作失败");

        TemHandlerConfig handlerConfig = findConfigByBusinTableType(businTableType);
        BTAssert.notNull(handlerConfig, "请先配置业务信息,操作失败");

        BaseService service = findBaseService(handlerConfig.getServerHandler());
        if (service != null && service instanceof IApplyTemDataService
                && StringUtils.isNoneBlank(handlerConfig.getApplyEffectiveProperties())) {
            // 获取到立即生效的属性map数据
            Map<String, Object> applyMap = convertPropertiesValueFromTemData(
                    handlerConfig.getApplyEffectiveProperties(), anTemData);

            IApplyTemDataService applyTemDataService = (IApplyTemDataService) service;
            if (addFlag) {
                applyTemDataService.handlerAdd(applyMap, paresObjectFromTemData(anTemData));
            } else {
                applyTemDataService.handlerUpdate(applyMap, paresObjectFromTemData(anTemData));
            }
        }

    }

    /**
     * 
     * @Title: saveSynchronizedDeleteBusin 
     * @Description: 同步删除业务表的数据 
     * @param temData 同步删除业务表的数据
     * @throws 
     * @author xuyp
     * @date 2017年12月25日 下午3:25:33
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void saveSynchronizedDeleteBusin(TemData anTemData) {

        BTAssert.notNull(anTemData, "数据为空,操作失败");
        String businTableType = anTemData.getBusinTableType();
        BTAssert.isTrue(StringUtils.isNoneBlank(businTableType), "请绑定数据类型,操作失败");

        TemHandlerConfig handlerConfig = findConfigByBusinTableType(businTableType);
        BTAssert.notNull(handlerConfig, "请先配置业务信息,操作失败");
        BaseService service = findBaseService(handlerConfig.getServerHandler());
        if (service != null) {

            if (service instanceof IDeleteTemDataService) {
                IDeleteTemDataService deleteService = (IDeleteTemDataService) service;
                deleteService.handlerDelete(paresObjectFromTemData(anTemData));
            }
        }
    }

    /**
     * 
     * @Title: saveSynchronizedCheck 
     * @Description: 同步复核操作 如果设置步骤复核直接生效会自动生效数据并且修改状态
     * @param temData 同步复核操作 如果设置步骤复核直接生效会自动生效数据并且修改状态
     * @throws 
     * @author xuyp
     * @date 2017年12月25日 下午4:56:21
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void saveSynchronizedCheck(TemData anTemData, Map<String, Object> anMap, TemHandlerConfig handlerConfig) {
        BTAssert.notNull(handlerConfig, "请先配置业务信息,操作失败");
        // 校验操作步骤

        String stepHandler = handlerConfig.getStepHandler();

        BTAssert.isTrue(StringUtils.isNotBlank(stepHandler), "配置操作步骤为空,操作失败");

        BTAssert.isTrue(!TemHandlerConfigConstant.CONFIGHANDLER_STEPHANDLER_ONLYAUDIT.equals(stepHandler),
                "配置操作步骤没有复核操作,操作失败");

        BTAssert.isTrue(TemDataConstant.TEMDATA_BUSINSTATUS_INSERTED.equals(anTemData.getBusinStatus()),
                "该数据已经进行了后续操作,操作失败");

        anTemData.setBusinStatus(TemDataConstant.TEMDATA_BUSINSTATUS_CHECKED);

        // 校验给定属性从json的值是否等于前端传递校验的值
        checkVerifyPropertiesValueComparable(handlerConfig.getCheckProperties(), anTemData.getBusinTableData(), anMap);

        BaseService service = findBaseService(handlerConfig.getServerHandler());
        if (service != null) {
            if (TemDataConstant.TEMDATA_HANDLERRESULT_YES.equals(anTemData.getHandlerResult())) {
                // 复核通过
                // 生效接口
                if (TemHandlerConfigConstant.CONFIGHANDLER_STEPHANDLER_ONLYCHECK.equals(stepHandler)) {
                    if (service instanceof IEffectiveTemDataService) {
                        IEffectiveTemDataService effectiveService = (IEffectiveTemDataService) service;
                        effectiveService.handlerEffective(paresObjectFromTemData(anTemData));
                    } else {
                        BTAssert.notNull(null, "操作接口没有实现生效接口,操作失败");
                    }
                    // 如果是复核直接生效
                    anTemData.setBusinStatus(TemDataConstant.TEMDATA_BUSINSTATUS_EFFECTIVE);
                }
                // 调用回调接口

                if (service instanceof ICheckTemDataService) {
                    ICheckTemDataService checkService = (ICheckTemDataService) service;
                    checkService.handlerCheck(paresObjectFromTemData(anTemData));
                }

            } else {
                // 处理审核不通过
                anTemData.setBusinStatus(TemDataConstant.TEMDATA_BUSINSTATUS_DELETED);
                /*if (service instanceof ICheckTemDataService) {
                    ICheckTemDataService checkService = (ICheckTemDataService) service;
                    checkService.handlerNoCheck(paresObjectFromTemData(anTemData));
                }*/
                if (service instanceof IEffectiveTemDataService) {
                    IEffectiveTemDataService effectiveService = (IEffectiveTemDataService) service;
                    effectiveService.handlerNoEffective(paresObjectFromTemData(anTemData));
                }
            }
        } else {
            BTAssert.notNull(null, "获取接口service对象失败,操作失败");
        }
    }

    /**
     * 
     * @Title: saveSynchronizedCheckOrAudit 
     * @Description: 进行审核或者复核操作
     * @param anTemData
     * @param anMap 
     * @throws 
     * @author xuyp
     * @date 2018年1月3日 下午6:02:59
     */
    public void saveSynchronizedCheckOrAudit(TemData anTemData, Map<String, Object> anMap) {

        BTAssert.notNull(anTemData, "数据为空,操作失败");
        String businTableType = anTemData.getBusinTableType();
        BTAssert.isTrue(StringUtils.isNoneBlank(businTableType), "请绑定数据类型,操作失败");

        TemHandlerConfig handlerConfig = findConfigByBusinTableType(businTableType);
        BTAssert.notNull(handlerConfig, "请先配置业务信息,操作失败");
        // 校验操作步骤
        String stepHandler = handlerConfig.getStepHandler();
        BTAssert.isTrue(StringUtils.isNotBlank(stepHandler), "配置操作步骤为空,操作失败");

        // 复核的标记
        boolean checkFlag = getCheckOperatorFlag(anTemData, handlerConfig);
        if (checkFlag) {
            saveSynchronizedCheck(anTemData, anMap, handlerConfig);
            return;
        }

        boolean auditFlag = getAuditOperatorFlag(anTemData, handlerConfig);

        if (auditFlag) {
            saveSynchronizedAudit(anTemData, anMap, handlerConfig);
            return;
        }

        BTAssert.notNull(null, "数据状态或配置信息出错,操作失败");
    }

    /**
     * 
     * @Title: getAuditOperatorFlag 
     * @Description: 审核的标记
     * @param anTemData 
     * @param handlerConfig
     * @return 当数据状态为1  配置步骤为3时   或者数据状态为2 配置步骤为4 
     * @throws 
     * @author xuyp
     * @date 2018年1月3日 下午5:53:47
     */
    private boolean getAuditOperatorFlag(TemData anTemData, TemHandlerConfig handlerConfig) {

        if (anTemData != null && handlerConfig != null) {
            if (TemDataConstant.TEMDATA_BUSINSTATUS_INSERTED.equals(anTemData.getBusinStatus())) {
                if (TemHandlerConfigConstant.CONFIGHANDLER_STEPHANDLER_ONLYAUDIT
                        .equals(handlerConfig.getStepHandler())) {
                    return true;
                }
            }

            if (TemDataConstant.TEMDATA_BUSINSTATUS_CHECKED.equals(anTemData.getBusinStatus())) {
                if (TemHandlerConfigConstant.CONFIGHANDLER_STEPHANDLER_CHECKTOAUDIT
                        .equals(handlerConfig.getStepHandler())) {
                    return true;
                }
            }

        }
        return false;
    }

    /**
     * 
     * @Title: getCheckOperatorFlag 
     * @Description: 执行 复核操作的条件   
     * 
     * @param anTemData  状态为1 
     * @param handlerConfig  stepHandler 为2  或者4
     * @return 
     * @throws 
     * @author xuyp
     * @date 2018年1月3日 下午5:46:01
     */
    private boolean getCheckOperatorFlag(TemData anTemData, TemHandlerConfig handlerConfig) {

        if (anTemData != null && handlerConfig != null) {
            if (TemDataConstant.TEMDATA_BUSINSTATUS_INSERTED.equals(anTemData.getBusinStatus())) {
                if (TemHandlerConfigConstant.CONFIGHANDLER_STEPHANDLER_ONLYCHECK.equals(handlerConfig.getStepHandler())
                        || TemHandlerConfigConstant.CONFIGHANDLER_STEPHANDLER_CHECKTOAUDIT
                                .equals(handlerConfig.getStepHandler())) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 
     * @Title: saveSynchronizedAudit 
     * @Description: 审核数据
     * @param temData 审核数据
     * @throws 
     * @author xuyp
     * @date 2017年12月25日 下午5:36:30
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void saveSynchronizedAudit(TemData anTemData, Map<String, Object> anMap, TemHandlerConfig handlerConfig) {

        BTAssert.notNull(handlerConfig, "请先配置业务信息,操作失败");
        // 校验操作步骤

        String stepHandler = handlerConfig.getStepHandler();
        BTAssert.isTrue(StringUtils.isNotBlank(stepHandler), "配置操作步骤为空,操作失败");
        BTAssert.isTrue(!TemHandlerConfigConstant.CONFIGHANDLER_STEPHANDLER_ONLYCHECK.equals(stepHandler),
                "配置操作步骤没有审核操作,操作失败");

        // 校验状态
        if (TemDataConstant.TEMDATA_BUSINSTATUS_INSERTED.equals(anTemData.getBusinStatus())) {
            if (TemHandlerConfigConstant.CONFIGHANDLER_STEPHANDLER_CHECKTOAUDIT.equals(stepHandler)) {
                BTAssert.notNull(null, "需要先复核之后才能进行审核,操作失败");
            }
        } else if (TemDataConstant.TEMDATA_BUSINSTATUS_CHECKED.equals(anTemData.getBusinStatus())) {

        } else {
            BTAssert.notNull(null, "该记录已经进行了操作,操作失败");
        }

        anTemData.setBusinStatus(TemDataConstant.TEMDATA_BUSINSTATUS_EFFECTIVE);

        // 校验给定属性从json的值是否等于前端传递校验的值
        checkVerifyPropertiesValueComparable(handlerConfig.getAuditProperties(), anTemData.getBusinTableData(), anMap);

        BaseService service = findBaseService(handlerConfig.getServerHandler());
        if (service != null) {
            if (TemDataConstant.TEMDATA_HANDLERRESULT_YES.equals(anTemData.getHandlerResult())) {
                if (service instanceof IEffectiveTemDataService) {
                    IEffectiveTemDataService effectiveService = (IEffectiveTemDataService) service;
                    effectiveService.handlerEffective(paresObjectFromTemData(anTemData));
                } else {
                    BTAssert.notNull(null, "操作接口没有实现生效接口,操作失败");
                }

                // 调用回调接口

                if (service instanceof IAuditTemDataService) {
                    IAuditTemDataService auditService = (IAuditTemDataService) service;
                    auditService.handlerAudit(paresObjectFromTemData(anTemData));
                }
            } else {
                // 处理审核不通过
                anTemData.setBusinStatus(TemDataConstant.TEMDATA_BUSINSTATUS_DELETED);
                /*if (service instanceof IAuditTemDataService) {
                    IAuditTemDataService auditService = (IAuditTemDataService) service;
                    auditService.handlerNoAudit(paresObjectFromTemData(anTemData));
                }*/
                if (service instanceof IEffectiveTemDataService) {
                    IEffectiveTemDataService effectiveService = (IEffectiveTemDataService) service;
                    effectiveService.handlerNoEffective(paresObjectFromTemData(anTemData));
                }
            }
        } else {
            BTAssert.notNull(null, "获取接口service对象失败,操作失败");
        }
    }

    /**
     * 
     * @Title: removeRedisHandlerConfig 
     * @Description: 从redis中移除配置中心的配置 
     * @param anTableType 类的全路径
     * @throws 
     * @author xuyp
     * @date 2017年12月27日 下午5:37:28
     */
    private void removeRedisHandlerConfig(String anTableType) {

        if (StringUtils.isNoneBlank(anTableType)) {

            CacheUtils.remove(TemHandlerConfigConstant.CONFIGHANDLER_STOR_REDIS_CACHE_NAME,
                    TemHandlerConfigConstant.CONFIGHANDLER_STORE_REDIS_PRIMARTKEY_PREFIX + anTableType);
        } else {
            CacheUtils.removeByPrefix(TemHandlerConfigConstant.CONFIGHANDLER_STOR_REDIS_CACHE_NAME,
                    TemHandlerConfigConstant.CONFIGHANDLER_STORE_REDIS_PRIMARTKEY_PREFIX);
        }

    }

    /**
     * 
     * @Title: checkVerifyPropertiesValueComparable 
     * @Description: 校验给定属性从json的值是否等于前端传递校验的值
     * @param checkProperties  配置的业务属性集合
     * @param businTableData   所有业务数据json
     * @param anMap 数据对比来源
     * @throws 
     * @author xuyp
     * @date 2017年12月27日 下午3:54:02
     */
    private void checkVerifyPropertiesValueComparable(String checkProperties, String businTableData,
            Map<String, Object> anMap) {
        if (StringUtils.isNoneBlank(checkProperties)) {

            BTAssert.isTrue(StringUtils.isNoneBlank(businTableData), "业务数据为空,操作失败");
            BTAssert.isTrue(!Collections3.isEmpty(anMap), "校验数据为空,操作失败");

            List<String> propertiesList = convertStringToList(checkProperties,
                    TemRelationConstant.SYSTEM_FILE_SPAREATOR);
            if (!Collections3.isEmpty(propertiesList)) {

                for (String properties : propertiesList) {
                    String propertiesValue = getPropertiesValue(properties, businTableData);
                    if (StringUtils.isBlank(propertiesValue)) {
                        break;
                    }
                    BTAssert.isTrue(anMap.containsKey(properties), "请传递参数" + properties + ",操作失败");
                    Object objectValue = anMap.get(properties);
                    BTAssert.isTrue(objectValue != null, "请传递参数" + properties + ",操作失败");
                    if (!objectValue.toString().equals(propertiesValue)) {
                        BTAssert.notNull(null, "校验参数" + properties + "的值失败,操作失败");
                    }
                }

            }
        }

    }

    /**
     * 
     * @Title: checkVerfiyDataPart 
     * @Description: 业务校验对象是否为空
     * @param anTemData
     * @param handlerConfig
     * @param handModel 校验处理模式 1新增修改校验 2 复核校验 3 审核校验
     * @throws 
     * @author xuyp
     * @date 2017年12月27日 下午2:15:27
     */
    private void checkVerfiyDataPart(TemData anTemData, TemHandlerConfig handlerConfig, int handModel) {

        BTAssert.notNull(anTemData, "校验数据为空,操作失败");
        BTAssert.notNull(handlerConfig, "校验数据为空,操作失败");
        BTAssert.isTrue(StringUtils.isNoneBlank(anTemData.getBusinTableData()), "业务数据为空,操作失败");
        switch (handModel) {

        case TemHandlerConfigConstant.CONFIGHANDLER_CHECKHANDLERMODEL_ADDORUPDATE:
            checkVerfiyDataPart(handlerConfig.getApplyEffectiveProperties(), anTemData.getBusinTableData());
            break;

        case TemHandlerConfigConstant.CONFIGHANDLER_CHECKHANDLERMODEL_CHECK:
            checkVerfiyDataPart(handlerConfig.getCheckProperties(), anTemData.getBusinTableData());
            break;

        case TemHandlerConfigConstant.CONFIGHANDLER_CHECKHANDLERMODEL_AUDIT:
            checkVerfiyDataPart(handlerConfig.getAuditProperties(), anTemData.getBusinTableData());
            break;

        default:
            break;
        }

        // getPropertiesValue(properties, anTemData.getBusinTableData());

    }

    /**
     * 
     * @Title: checkVerfiyDataPart 
     * @Description: 校验单个属性值不为空 
     * @param auditProperties  业务属性集合(业务属性,业务属性)
     * @param businTableData 业务数据json
     * @throws 
     * @author xuyp
     * @date 2017年12月27日 下午2:29:58
     */
    private void checkVerfiyDataPart(String auditProperties, String businTableData) {

        if (StringUtils.isNoneBlank(auditProperties) && StringUtils.isNoneBlank(businTableData)) {

            List<String> propertiesList = convertStringToList(auditProperties,
                    TemRelationConstant.SYSTEM_FILE_SPAREATOR);
            if (!Collections3.isEmpty(propertiesList)) {
                for (String properties : propertiesList) {
                    String propertiesValue = getPropertiesValue(properties, businTableData);

                    BTAssert.isTrue(StringUtils.isNoneBlank(propertiesValue), properties + "业务数据为空,操作失败");
                }

            }

        }

    }

    /**
     * 
     * @Title: convertPropertiesValueFromTemData 
     * @Description: 获取属性的值map  key properties   value  属性值
     * @param applyEffectiveProperties
     * @param anTemData
     * @return key properties   value  属性值
     * @throws 
     * @author xuyp
     * @date 2017年12月27日 下午3:04:48
     */
    private Map<String, Object> convertPropertiesValueFromTemData(String applyEffectiveProperties, TemData anTemData) {

        if (StringUtils.isNoneBlank(applyEffectiveProperties)
                && StringUtils.isNoneBlank(anTemData.getBusinTableData())) {

            List<String> propertiesList = convertStringToList(applyEffectiveProperties,
                    TemRelationConstant.SYSTEM_FILE_SPAREATOR);
            if (!Collections3.isEmpty(propertiesList)) {
                Map<String, Object> map = new HashMap<>(propertiesList.size());

                for (String properties : propertiesList) {
                    String propertiesValue = getPropertiesValue(properties, anTemData.getBusinTableData());
                    if (StringUtils.isNoneBlank(propertiesValue)) {
                        map.put(properties, propertiesValue);
                    }
                }

                return map;
            }

        }

        return null;
    }

    /**
     * 
     * @Title: paresObjectFromTemData 
     * @Description: 解析json数据成指定对象 
     * @param anTemdata
     * @return 解析json数据
     * @throws 
     * @author xuyp
     * @date 2017年12月25日 下午5:35:54
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private Object paresObjectFromTemData(TemData anTemdata) {

        BTAssert.notNull(anTemdata, "数据为空,操作失败");

        try {
            Class clazz = Class.forName(anTemdata.getBusinTableType());
            return JSONObject.parseObject(anTemdata.getBusinTableData(), clazz);
        }
        catch (ClassNotFoundException e) {
            logger.info("反射出错" + e.getMessage());
        }

        return null;

    }

    /**
     * 
     * @Title: findBusinTablePrimaryKeyFromTemData 
     * @Description: 返回临时数据对应业务表的数据
     * @param anTemData
     * @return 返回临时数据对应业务表的数据
     * @throws 
     * @author xuyp
     * @date 2017年12月26日 下午5:13:32
     */
    @SuppressWarnings("unused")
    private Object findBusinTablePrimaryKeyFromTemData(TemData anTemData) {

        BTAssert.notNull(anTemData, "操作数据为空,操作失败");
        try {

            Class<?> clazz = Class.forName(anTemData.getBusinTableType());
            Object object = clazz.newInstance();
            // 封装主键
            Set<EntityColumn> pkColumns = EntityHelper.getPKColumns(clazz);
            // 主键名称
            // 主键类型
            Class<?> javaType = String.class;
            for (EntityColumn entityColumn : pkColumns) {
                javaType = entityColumn.getJavaType();
            }
            return objectValueJavaTypeConvert(anTemData.getBusinTableId(), javaType);
        }
        catch (Exception e) {

        }

        return anTemData.getBusinTableId();
    }

    /**
     * 
     * @Title: objectValueJavaTypeConvert 
     * @Description: 值java类型转换
     * @param value
     * @param type
     * @return 值java类型转换
     * @throws 
     * @author xuyp
     * @date 2017年12月26日 下午4:22:13
     */
    private Object objectValueJavaTypeConvert(Object value, Class<?> type) {
        try {
            if (type != null) {
                String name = type.getName().toUpperCase();
                if (name.contains("STRING")) {
                    value = value.toString();
                } else if (name.contains("LONG")) {
                    value = Long.parseLong(value.toString());
                } else if (name.contains("BOOLEAN")) {
                    value = Boolean.parseBoolean(value.toString());
                } else if (name.contains("INT")) {
                    value = Integer.parseInt(value.toString());
                } else if (name.contains("DOUBLE")) {
                    value = Double.parseDouble(value.toString());
                } else if (name.contains("FLOAT")) {
                    value = Float.parseFloat(value.toString());
                }
            }
        }
        catch (Exception e) {
            logger.info("转换数据类型出错" + e.getMessage());
            BTAssert.notNull(null, e.getMessage());
        }
        return value;
    }

    /**
     * 
     * @Title: getPropertiesValue 
     * @Description: 查询string对象里面的值
     * @param string
     * @param businTableData
     * @return 查询string对象里面的值
     * @throws 
     * @author xuyp
     * @date 2017年12月25日 上午9:48:50
     */
    private String getPropertiesValue(String properties, String jsonData) {

        if (StringUtils.isNoneBlank(properties) && StringUtils.isNoneBlank(jsonData)) {
            JSONObject parseObject = JSONObject.parseObject(jsonData);
            return parseObject.getString(properties);
        }
        return null;
    }

    /**
     * 
     * @Title: convertStringToList 
     * @Description: 将与,结合的属性分割成集合对象
     * @param anValue
     * @param anSpareator
     * @return 将与,结合的属性分割成集合对象
     * @throws 
     * @author xuyp
     * @date 2017年12月25日 上午9:37:12
     */
    private List<String> convertStringToList(String anValue, String anSpareator) {

        if (StringUtils.isNoneBlank(anValue)) {

            // 如果分割符为空，选用默认分割符,
            if (StringUtils.isBlank(anSpareator)) {
                anSpareator = TemRelationConstant.SYSTEM_FILE_SPAREATOR;
            }

            List<String> stringList = new ArrayList<>();
            if (anValue.contains(anSpareator)) {

                String[] split = anValue.split(anSpareator);
                for (String string : split) {
                    if (StringUtils.isNoneBlank(string)) {
                        stringList.add(string);
                    }
                }

            } else {
                stringList.add(anValue);
            }

            return stringList;
        }

        return Lists.newArrayList();
    }

    /**
     * 
     * @Title: findBaseService 
     * @Description: 查找service对象
     * @param handlerConfig
     * @return 查找service对象
     * @throws 
     * @author xuyp
     * @date 2017年12月25日 上午10:34:16
     */
    @SuppressWarnings("rawtypes")
    private BaseService findBaseService(String anServiceUrl) {
        BaseService baseService = null;
        try {
            if (StringUtils.isNoneBlank(anServiceUrl)) {

                Object beanService = applicationContext.getBean(Class.forName(anServiceUrl));
                if (beanService != null && beanService instanceof BaseService) {
                    baseService = (BaseService) beanService;
                }
            }
        }
        catch (Exception e) {
            logger.info("查找service对象出错" + e.getMessage());
        }

        return baseService;
    }

    @SuppressWarnings("static-access")
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        removeRedisHandlerConfig("");

    }

}
